package de.haw.HES.base;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import de.haw.HES.Warenmanagement.pub.ProduktmanagementAdapterService;

import de.haw.HES.Disposition.pub.DispositionAdapterService;

public class RmiServerHES extends RmiStarter {
	
	private AvailableServer availabilityThread;
	private String instanceIdentifier;

	public RmiServerHES(String identifier) {

		super(DispositionAdapterService.class,identifier);

		this.instanceIdentifier = identifier;
		
//		availabilityThread = new DefaultAvailableServer(null, null,instanceIdentifier);
		availabilityThread = new DefaultAvailableServer();
//		((DefaultAvailableServer) availabilityThread).run();
	}

	@Override
    public void doCustomRmiHandling(String s) {
        try {
        	DispositionAdapterService engine = (DispositionAdapterService) ApplicationContextProvider.getApplicationContext().getBean("de.haw.HES.Disposition.pub.DispositionAdapterService");
        	DispositionAdapterService engineStub = (DispositionAdapterService)UnicastRemoteObject.exportObject(engine, 0);
        	
//        	ProduktmanagementAdapterService engine2 = (ProduktmanagementAdapterService) ApplicationContextProvider.getApplicationContext().getBean("de.haw.HES.Warenmanagement.pub.ProduktmanagementAdapterService");
//        	ProduktmanagementAdapterService engineStub2 = (ProduktmanagementAdapterService)UnicastRemoteObject.exportObject(engine2, 0);
        	
            Registry registry = LocateRegistry.getRegistry();
            
            registry.rebind(s+DispositionAdapterService.SERVICE_NAME, engineStub);
//            registry.rebind(DispositionAdapterService.SERVICE_NAME, engineStub2);
            
        }
        catch(Exception e) {
            e.printStackTrace();
        }

    }
	
	public static void main(String[] args) {
		System.out.println("Server initializin'");
		try {
//			new RmiServerHES("Test");
			new RmiServerHES("Test2");
		} catch(Exception ex) {
			System.err.println("Server init crashed");
			System.err.println(ex.getMessage());
		}
		System.out.println("Server up and running");
	}
	
}
